package com.sjlh.centurylianhua.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.User;
import com.sjlh.centurylianhua.mapper.UserMapper;
import com.sjlh.centurylianhua.utils.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Resource
    private UserMapper userMapper;
    /**
     * 分页查询
     *
     * */
    @RequestMapping("/getAll")
    public Object getAll(@RequestBody SearchCon searchCon){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isnotNull(searchCon.getName()), "userName", searchCon.getName());
        wrapper.like(StringUtils.isnotNull(searchCon.getName2()),"sex",searchCon.getName2());
        // 封装分页对象
        Page<User> page = new Page<User>(searchCon.getPage(), searchCon.getLimit());
        Page<User> userPage = userMapper.selectPage(page, wrapper);
        List<User> list=userPage.getRecords();//所有数据
        long count=userPage.getTotal();//总条数
        ResultData rs=new ResultData(true,20000,"成功",list,count);
        return rs;
    }
    /**
     *根据员工名字精确查询员工
     * */
    public User getOne(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq( "userName", user.getUserName());
        return userMapper.selectOne(wrapper);
    }

    /**
     *
     * */
    @RequestMapping("/add")
    public Object add(@RequestBody User user){
        User d = this.getOne(user);
        if (d == null) {
            int r=userMapper.insert(user);
            if(r==1)
                return new ResultData(true,20000,"添加员工成功",true,null);
            else
                return new ResultData(false,20000,"添加员工失败",false,null);
        } else {
            return new ResultData(false,20001,"员工名已存在",false,null);
        }

    }
    /**
     *
     * */
    @RequestMapping("/set")
    public Object set(@RequestBody User user){
        User u = this.getOne(user);
        if (u != null) {
            int r = userMapper.updateById(user);
            System.out.println(user);
            if(r==1)
                return new ResultData(true,20000,"修改员工成功",true,null);
            else
                return new ResultData(false,20000,"修改员工失败",false,null);
        } else {
            return new ResultData(false,20001,"修改过的员工名已存在",null,null);
        }

    }
    /**
     *
     * */
    @RequestMapping("/del")
    public Object del(@RequestBody User user){
        int rs = userMapper.deleteById(user.getId());

        if(rs==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20001,"删除失败",false,null);
    }

    /**
     * 用户登录接口
     *
     * @param un  用户
     * @param pwd 密码
     * @return
     */
    @PostMapping("/login")
    public Object login(@RequestBody User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("account", user.getAccount());
        User u = userMapper.selectOne(wrapper);
        System.out.println(user.getAccount());
        System.out.println(user.getPassword());
        if (u == null) {
            return Result.error();
        } else {

                if (u.getPassword().equals(user.getPassword())) {
                    String token = JwtUtils.getToken(user.getAccount());
                    return new Resultlogin(ResultCode.SUCCESS,user,true,token);
                } else {

                    return Result.error();
                }

        }

    }

    @GetMapping("/info")  // "token:xxx"
    public Resultlogin info(String token){
        System.out.println("token:"+token);
        String account = JwtUtils.parseToken(token).getSubject();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account",account);
        List<User> list = userMapper.selectList(wrapper);
        return new Resultlogin(ResultCode.SUCCESS,list,true,token);
    }

    @PostMapping("/logout")  // "token:xxx"
    public Result logout(){
        return Result.ok();
    }
}
